package Modules::Utilities;

sub new {
  my ($class) = @_;
  my $self = {
               CONFIG_FILE_PATH => '',
               TIME_START       => time,
             };
  bless $self,$class;
  return $self;
}

sub time_elapsed {
  my $self = shift;
  return time - $self->{TIME_START};
}

sub read_conf_file {
  my ($self,$conf,$conf_file_type) = @_;
  $self->{'CONFIG_FILE_PATH'}      = $conf;
  open (CONFIG,"<$conf") or die "cannot find configuration file: $!";
  while (<CONFIG>) {
    chomp;                  # no newline
    s/#.*//;                # no comments
    s/^\s+//;               # no leading white
    s/\s+$//;               # no trailing white
    next unless length;     # anything left?
    my ($param, $value) = split(/\s*=\s*/, $_, 2);
    
    $value = '' if !defined $value;
    $self->{$param} = $value;
  } 
  close(CONFIG);
  return ;
}

sub decodeSAMFlag  {
  my $flag = $_[0];
  $flag = sprintf ("%012b", $flag);
  my @flags = split (//, $flag);
  my $flagInfo = {
                   PE          =>$flags[11],
                   PE_map      =>$flags[10],
                   query_map   =>$flags[9],
                   mate_map    =>$flags[8],
                   query_strand=>$flags[7] == 0 ? '+' : '-',
                   mate_strand =>$flags[6] == 0 ? '+' : '-',
                   read_1_or_2 =>$flags[5] == 1 ? 'read_1' : 'read_2' };
  return $flagInfo;
}


1;